package com.chenxi.provider;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.*;


public class MyClientDetails implements ClientDetails {

    private String clientId;
    private String resourceIds;
    private boolean secretRequired;
    private String clientSecret;
    private boolean scoped;
    private String scope;
    private  String authorizedGrantTypes;
    private String registeredRedirectUri;
    private Collection<GrantedAuthority> authorities = new HashSet<>();
    private Integer accessTokenValiditySeconds;
    private Integer refreshTokenValiditySeconds;
    private boolean autoApprove;

    private Map<String,Object> additionalInformation;

    @Override
    public String getClientId() {
        return clientId;
    }

    @Override
    public Set<String> getResourceIds() {
        if(resourceIds!=null&&!resourceIds.isEmpty()){
            Set<String> set = new HashSet<>(Arrays.asList(resourceIds.split(",")));
            return set;
        }
        return null;
    }

    @Override
    public boolean isSecretRequired() {
        return secretRequired;
    }

    @Override
    public String getClientSecret() {
        return clientSecret;
    }

    @Override
    public boolean isScoped() {
        return scoped;
    }

    @Override
    public Set<String> getScope() {
        if(scope!=null&&!scope.isEmpty()){
            Set<String> set = new HashSet<>(Arrays.asList(scope.split(",")));
            return set;
        }
        return null;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        if(authorizedGrantTypes!=null&&!authorizedGrantTypes.isEmpty()){
            Set<String> set = new HashSet<>(Arrays.asList(authorizedGrantTypes.split(",")));
            return set;
        }
        return null;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        if (registeredRedirectUri != null && !registeredRedirectUri.isEmpty()) {
            Set<String> set = new HashSet<>(Arrays.asList(registeredRedirectUri.split(",")));
            return set;
        }
        return null;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return accessTokenValiditySeconds;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return refreshTokenValiditySeconds;
    }

    @Override
    public boolean isAutoApprove(String scope) {
        return autoApprove;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return additionalInformation;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public void setResourceIds(String resourceIds) {
        this.resourceIds = resourceIds;
    }

    public void setSecretRequired(boolean secretRequired) {
        this.secretRequired = secretRequired;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public void setScoped(boolean scoped) {
        this.scoped = scoped;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public void setAuthorizedGrantTypes(String authorizedGrantTypes) {
        this.authorizedGrantTypes = authorizedGrantTypes;
    }

    public void setRegisteredRedirectUri(String registeredRedirectUri) {
        this.registeredRedirectUri = registeredRedirectUri;
    }

    public void setAuthorities(Collection<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setAccessTokenValiditySeconds(Integer accessTokenValiditySeconds) {
        this.accessTokenValiditySeconds = accessTokenValiditySeconds;
    }

    public void setRefreshTokenValiditySeconds(Integer refreshTokenValiditySeconds) {
        this.refreshTokenValiditySeconds = refreshTokenValiditySeconds;
    }

    public void setAutoApprove(boolean autoApprove) {
        this.autoApprove = autoApprove;
    }

    public void setAdditionalInformation(Map<String, Object> additionalInformation) {
        this.additionalInformation = additionalInformation;
    }
}
